package com.hxbd.gxsec_data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxbd.gxsec_data.dao.ResourceDao;
import com.hxbd.gxsec_data.pojo.FileDO;
import com.hxbd.gxsec_data.pojo.ResourceDO;
import com.hxbd.gxsec_data.service.ResourceService;
import com.hxbd.gxsec_data.utils.FileUtil;
import com.hxbd.gxsec_data.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ResourceServiceimpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public FileDO uploadFiles(MultipartFile file) {
        String oldName = file.getOriginalFilename();
        String newName = FileUtil.renameToUUID(oldName);
        try {
            minioUtil.putObject("file",file,newName);
            String id = UUID.randomUUID().toString().replaceAll("-","");
            String url = "http://gxsec.org.cn/staticFile/file/"+newName;
            FileDO fileDO = FileDO.builder().fileId(id).fileurl(url).filename(oldName).build();
            return  resourceDao.uploadFiles(fileDO)?fileDO:null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean saveAddResouce(Map<String, Object> param) {
        param.put("resourceId",UUID.randomUUID().toString().replaceAll("-",""));
        return resourceDao.saveAddResouce(param)&&resourceDao.bindingResource(param);
    }

    @Override
    public List<JSONObject> getAllResource() {
        return resourceDao.getAllResource();
    }

    @Override
    public boolean deleteresource(String rsourceId, String fileName) {
        if(redisTemplate.hasKey("searchResource")){
            redisTemplate.delete("searchResource");
        }
        return minioUtil.removeObject("file",fileName)
                &&resourceDao.deleteresource(rsourceId);
    }

    @Override
    public Integer countTheNumberOfPageVisits(String id) {
        if(redisTemplate.hasKey("countResouceDown")&&redisTemplate.opsForHash().hasKey("countResouceDown",id)){
                ResourceDO resourceDO = (ResourceDO)redisTemplate.opsForHash().get("countResouceDown",id);
                ResourceDO resourceDO1 = ResourceDO.builder().resourceId(id).count(resourceDO.getCount()+1).build();
                redisTemplate.opsForHash().put("countResouceDown",id,resourceDO1);
                return resourceDO1.getCount();
        }else {
            ResourceDO resourceDO = ResourceDO.builder().resourceId(id).count(1).build();
            Map<String,ResourceDO> map = new HashMap<>();
            map.put(resourceDO.getResourceId(),resourceDO);
            redisTemplate.opsForHash().putAll("countResouceDown",map);
            return 1;
        }
    }

    @Override
    public List<JSONObject> searchResource(String name) {
        if(redisTemplate.hasKey("searchResource")&&redisTemplate.opsForHash().hasKey("searchResource",name)){
            return (List<JSONObject>)redisTemplate.opsForHash().get("searchResource",name);
        }else {
            List<JSONObject>  list = resourceDao.searchResource(name);
            redisTemplate.opsForHash().put("searchResource",name,list);
            return list;
        }
    }
}
